<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Copyright (c) 2017 http://www.bojuwang.net All rights reserved.
 * Created by PhpStorm.
 * Date: 2018/4/23 0023
 * Time: 下午 3:22
 * Author: NanTao <260053530@qq.com>
 */
namespace app\api\controller;
use think\Db;
use think\Request;
class User extends Common
{
   
    /** 
    * 获取用户信息
    * @access public
    * @param int $userId 用户id
    */
    public function get_user_id()
    {
        $userId = input('param.userId');
        if(!$userId){
            json_msg(1,'请求错误');
        }
        $user = Db::name('user')->field('id,avatar,email,mobile,sex,user_name,user_nickname,is_partner,partner_name,partner_mobile,partner_city,point,balance')->where('id',$userId)->find();
        if(!$user){
            json_msg(1,'用户不存在');
        }
        $user = edit_img_url($user,'avatar',2);
        $msgcount = Db::name('sys_message')->where(['user_id'=>$userId,'is_read'=>0])->count();
        $data['user_info'] = $user;
        $data['msgcount'] = $msgcount;
        json_msg_data(0,'ok',$data);
    }
   /** 
    * 修改用户资料
    * @access public
    * @param int $userId 用户id
    * @param int $avatar 头像
    * @param int $user_nickname 用户昵称
    * @param int $mobile 电话号码
    * @param int $email 邮箱
    * @param int $sex 性别
    */
    public function post_user_id()
    {
     
        $userId = input('param.userId');
        if(!$userId){
            json_msg(1,'请求错误');
        }
        $data = input('param.');
        unset($data['userId']);
        $res = Db::name('user')->where('id', $userId)->update($data);
        if ($res > 0 || $res == 0) {
            json_msg(0,'修改成功');
        } else {
            json_msg(1,'修改失败');
        }
      
    }

    /** 
    * 获取用户站内信
    * @access public
    * @param int $userId 用户id
    */
    public function get_msg_list()
    {
        $userId = input('param.userId');
        if(!$userId){
            json_msg(1,'请求错误');
        }
        $list = Db::name('sys_message')->where('user_id',$userId)->select();
        json_msg_data(0,'ok',$list);
    }
    /** 
    * 获取站内信详情
    * @access public
    * @param int $messageId 站内信id
    */
    public function get_msg_id()
    {
        $messageId = input('param.messageId');
        if(!$messageId){
            json_msg(1,'请求错误');
        }
        $info = Db::name('sys_message')->where('id',$messageId)->find();
        Db::name('sys_message')->where('id',$messageId)->setField('is_read',1);
        if($info){
            json_msg_data(0,'ok',$info);
        }else{
            json_msg(1,'信息不存在');
        }
    }
    /** 
    * 收货地址
    * @access public
    * @param int $userId 用户id
    */
    public function get_address_list()
    {
        $userId = input('param.userId');
        if(!$userId){
            json_msg(1,'请求错误');
        }
        $list = Db::name('user_address')->where('user_id',$userId)->order('is_default desc,id desc')->select();
        foreach ($list as $k=>$v)
        {
            $list[$k]['province'] = province($v['province_code']);
            $list[$k]['city'] = city($v['city_code']);
            $list[$k]['area'] = area($v['area_code']);
        }
        json_msg_data(0,'ok',$list);
    }

    /** 
    * 收货地址详情
    * @access public
    * @param int $addressId 用户收货地址id
    */
    public function get_address_id()
    {
        $addressId = input('param.addressId');
        if(!$addressId){
            json_msg(1,'请求错误');
        }
        $info = Db::name('user_address')->where('id',$addressId)->find();
        if(!$info){
            json_msg(1,'信息不存在');
        }
        $info['province'] = province($info['province_code']);
        $info['city'] = city($info['city_code']);
        $info['area'] = area($info['area_code']);
        json_msg_data(0,'ok',$info);
    }

    /**
    *添加收获地址
    * @access public
    * @param int $userId 用户id
    * @param int $name 姓名
    * @param int $mobile 电话号码
    * @param int $provinceid 省id
    * @param int $cityid 市id
    * @param int $areaid 区id
    * @param int $info 详细地址
    */
    public function add_address()
    {
        $userId = input('param.userId');
        $name = input('param.name');
        $mobile = input('param.mobile');
        $province = input('param.provinceid');
        $city = input('param.cityid');
        $area = input('param.areaid');
        $info = input('param.info');

        if(!$userId || !$name ||!$mobile ||!$province ||!$city ||!$area ||!$info){
            json_msg(1,'请求错误');
        }
        
        if(!Db::name('user_address')->where('user_id',$userId)->find()){
            $isDefault = 1;
        }else{
            $isDefault = 0;
        }
        $data = [
            'name'=>$name,
            'mobile'=>$mobile,
            'province_code'=>$province,
            'city_code'=>$city,
            'area_code'=>$area,
            'info'=>$info,
            'is_default'=>$isDefault,
            'user_id'=>$userId,
        ]; 
        if (Db::name('user_address')->insert($data)){
            json_msg(0,'添加成功');
        } else {
            json_msg(1,'添加失败');
        }
    }

    /**
    *修改收获地址
    * @access public
    * @param int $addressId 收货地址id
    * @param int $name 姓名
    * @param int $mobile 电话号码
    * @param int $provinceid 省id
    * @param int $cityid 市id
    * @param int $areaid 区id
    * @param int $info 详细地址
    */
    public function edit_address()
    {
        $addressId = input('param.addressId');
        $name = input('param.name');
        $mobile = input('param.mobile');
        $province = input('param.provinceid');
        $city = input('param.cityid');
        $area = input('param.areaid');
        $info = input('param.info');

        if(!$addressId || !$name ||!$mobile ||!$province ||!$city ||!$area ||!$info){
            json_msg(1,'请求错误');
        }
        
        $data = [
            'name'=>$name,
            'mobile'=>$mobile,
            'province_code'=>$province,
            'city_code'=>$city,
            'area_code'=>$area,
            'info'=>$info,
        ]; 
        $res = Db::name('user_address')->where('id',$addressId)->update($data);
        if ($res>=0){
            json_msg(0,'修改成功');
        } else {
            json_msg(1,'修改失败');
        }
    }
  
    /**
    *修改收获地址默认状态
    * @access public
    * @param int $addressId 收货地址id
    */
    public function edit_address_default()
    {
        $addressId = input('param.addressId');

        if(!$addressId){
            json_msg(1,'请求错误');
        }
        $user_id = Db::name('user_address')->where('id',$addressId)->value('user_id');
        Db::name('user_address')->where('user_id',$user_id)->update(['is_default'=>0]);
        $data = [
            'is_default'=>1
        ]; 
        $res = Db::name('user_address')->where('id',$addressId)->update($data);
        if ($res>=0){
            json_msg(0,'修改成功');
        } else {
            json_msg(1,'修改失败');
        }
    }
  
    /**
    *修改收获地址默认状态
    * @access public
    * @param int $addressId 收货地址id
    */
    public function delete_address()
    {
        $addressId = input('param.addressId');
        if(!$addressId){
            json_msg(1,'请求错误');
        }
        if (Db::name('user_address')->delete($addressId))
        {
            json_msg(0,'删除成功');
        } else {
            json_msg(1,'删除失败');
        }
    }

    /**
    *用户实名认证
    * @access public
    * @param int $userId 用户id
    * @param int $trueName 真实姓名
    * @param int $mobile 手机号
    * @param int $card 身份证号
    * @param int $code 验证码
    */
    public function post_certify()
    {
        $userId = input('param.userId');
        $trueName = input('param.trueName');
        $mobile = input('param.mobile');
        $code = input('param.code');
        $code_id = input('param.code_id');
        $card = input('param.card');
        if(!$userId || !$trueName ||!$mobile ||!$code ||!$code_id ||!$card){
            json_msg(1,'请求错误');
        }
        $sms_info = Db::name('sendsms_log')->where('id',$code_id)->find();
        if($sms_info['code']!=$code){
            json_msg(1,'验证码错误');
        }
        if($sms_info['mobile']!=$mobile){
            json_msg(1,'手机号不是获取验证码的手机号');
        }
        $info = Db::name('user')->find($userId);
        if(!$info){
            json_msg(1,'用户不存在');
        }
        if($info['is_true_name'] == 1){
            json_msg(1,'该用户已审核状态');
        }
        if($info['is_true_name'] == 2){
            json_msg(1,'该用户已认证成功');
        }
        $data = [
            'true_name'=>$trueName,
            'mobile'=>$mobile,
            'card'=>$card,
            'is_true_name'=>1
        ];
        $result = Db::name('user')->where('id',$userId)->update($data);
        if ($result>0)
        {
            json_msg(0,'认证已提交,等待后台审核');
        } else {
            json_msg(1,'提交失败');
        }
    }

  
    /**
    *获取用户银行卡信息
    * @access public
    * @param int $userId 用户id
    */
    public function get_user_bank()
    {
        $userId = input('param.userId');
        if(!$userId){
            json_msg(1,'请求错误');
        }
        $info = Db::name('user')->find($userId);
        if(!$info){
            json_msg(1,'用户不存在');
        }
        // if($info['is_true_name'] != 2){
        //     json_msg(1,'该用户未认证成功');
        // }
        $list = Db::name('user_bank')->where('user_id',$userId)->field('id,name,account,bank')->select();
        json_msg_data(0,'ok',$list);
     
    }
    /**
    *添加银行卡
    * @access public
    * @param int $userId 用户id
    * @param int $name 姓名
    * @param int $account 银行卡号
    * @param int $bank 开户行
    */
    public function add_user_bank()
    {
        $userId = input('param.userId');
        $name = input('param.name');
        $account = input('param.account');
        $bank = input('param.bank');
        if(!$userId || !$name ||!$account ||!$bank){
            json_msg(1,'请求错误');
        }
        $data['user_id'] = $userId;
        $data['name'] = $name;
        $data['account'] = $account;
        $data['bank'] = $bank;
        if (Db::name('user_bank')->insert($data))
        {
            json_msg(0,'绑定成功');
        } else {
            json_msg(1,'绑定失败');
        }
    }
    /**
     * 删除银行卡
     * @access public
     * @param int $bankId 银行卡id
     */
    public function delete_bank()
    {
        $bankId = input('param.bankId');
        if(!$bankId){
            json_msg(1,'请求错误');
        }
        if (Db::name('user_bank')->delete($bankId))
        {
            json_msg(0,'解绑成功');
        } else {
            json_msg(1,'解绑失败');
        }
    }
    /**
     * 用户账户信息
     * @access public
     * @param int $userId 用户id
     */
    public function get_user_account()
    {
        $userId = input('param.userId');
        if(!$userId){
            json_msg(1,'请求错误');
        }
        $userBalance = get_user_info_data($userId,'balance');
        $data['userBalance'] = $userBalance;
        $data['balance'] = Db::name('user_balance')->where('user_id',$userId)->order('add_time desc')->select();
        $data['cash'] = Db::name('user_cash')->where('user_id',$userId)->order('add_time desc')->select();
        json_msg_data(0,'ok',$data);
    }

    /**
    * 用户体现
    * @access public
    * @param int $userId 用户id
    * @param int $bankId 银行卡id
    * @param int $money 体现金额
    */
    public function post_user_cash()
    {
        $userId = input('param.userId');
        $bankId = input('param.bankId');
        $money = input('param.money');
        if(!$userId || !$bankId ||!$money){
            json_msg(1,'请求错误');
        }
        $bank = Db::name("user_bank")->where('id',$bankId)->find();
        $userBalance = get_user_info_data($userId,'balance');
        if ($userBalance < $money)
        {
            json_msg(1,'您的账户可用余额不足');
        }
        $userFrozen = get_user_info_data($userId,'frozen');
        $frozen = $userFrozen + $money;
        $balance = $userBalance - $money;
        $result =  Db::name('user')->where('id',$userId)->update(['balance'=>$balance,'frozen'=>$frozen]);
        if ($result) {
            $cash_cost = (sysconf('cash_cost')/100) * $money;
            $cash_money = $money - $cash_cost - sysconf('cash_tax');
            $data = [
                'user_id' => $userId,
                'money' => $money,
                'user_bank_id' => $bankId,
                'bank_account' => $bank['account'],
                'bank_name' => $bank['name'],
                'bank_bank'=>$bank['bank'],
                'cash_cost'=>$cash_cost,
                'cash_money'=>$cash_money,
                'cash_tax'=>sysconf('cash_tax'),
            ];
            Db::name('user_cash')->insert($data);
            $cashId = Db::name('user_cash')->getLastInsID();
            post_user_balance_log($userId,'-',$money,"申请提现{$money}元",1,$cashId);
            json_msg(0,'您的提现申请已提交到后台,请耐心等待!!!');
        } else {
            json_msg(1,'申请失败');
        }
    }

    /**
    * 获取文章列表
    * @access public
    * @param int $category_id 文章分类id
    */
    public function get_article_list()
    {
        $category_id = input('param.category_id');
        $data = get_article_data($category_id);
        json_msg_data(0,'ok',$data);
    }
    /**
    * 获取文章详情
    * @access public
    * @param int $category_id 文章分类id
    * @param int $article_id 文章id
    */
    public function get_article_id()
    {
        $article_id = input('param.article_id');
        $category_id = input('param.category_id');
        if (isset($category_id))
        {
            $data = get_article_data($category_id);
            $info = $data['list'][0];
        }
        if (isset($article_id)) {
            $info = Db::name('article')->find($article_id);
        }
        json_msg_data(0,'ok',$info);
    }
   
  
    /**
    * 获取用户下级
    * @access public
    * @param int $userId 用户id
    */
    public function get_user_agent()
    {
        $userId = input('param.userId');
        if(!$userId){
            json_msg(1,'请求错误');
        }
        $data = get_user_agent($userId);
        foreach ($data as $k => $v) {
            if(!$v['avatar']){
                $data[$k]['avatar'] = '/public/index/images/logo.png';
            }

            if(!$v['mobile']){
                $data[$k]['mobile'] = '未设置手机号';
            }
        }
        $data = edit_img_url($data,'avatar',1);
        json_msg_data(0,'ok',$data);
    }
   
    

    /**
    * 获取用户分销二维码
    * @access public
    * @param int $userId 用户id
    */
    public function get_user_qrcode()
    {
        $userId = input('param.userId');
        if(!$userId){
            json_msg(1,'请求错误');
        }
        $savepath = 'public/upload/userqcode';//存放在当前目录的upload文件夹下
        $savename = get_user_info_data($userId,'user_name').".jpg";
        $path = $savepath.'/'.$savename;
        $url = sysconf('website')."/index/index/index/parentId/".$userId;
        $qrcode = $this->createQrcode($url,$path,'');
        $user = get_user_model($userId);
        $user = edit_img_url($user,'avatar',2);
        $data['user'] = $user;
        $data['qrcode'] = sysconf('website').$qrcode;
        json_msg_data(0,'ok',$data);
    }

    /**
    * 合伙人申请
    * @access public
    * @param int $user_id 用户id
    */
    public function partner()
    {
      
        $partner_name = input('param.partner_name');
        $partner_mobile = input('param.partner_mobile');
        $partner_city = input('param.partner_city');
        $user_id = input('param.user_id');
        if(!$partner_name || !$partner_mobile ||!$partner_city ||!$user_id){
            json_msg(0,'请求错误');
        }

        $data = [
            'partner_name'=>$partner_name,
            'partner_mobile'=>$partner_mobile,
            'partner_city'=>$partner_city,
            'is_partner'=>1
        ];
        $result = Db::name('user')->where('id',$user_id)->update($data);
        if ($result>0)
        {
            json_msg(0,'认证已提交,等待后台审核');
        } else {
            json_msg(1,'提交失败');
        }
       
    }

    
    /**
     * 合伙人业绩统计
     * @access public
     * @param int $user_id 用户id
     */
    public function partnerlist()
    {
        $user_id = input('param.user_id');
        if(!$user_id){
            json_msg(0,'请求错误');
        }
        $user_list =get_user_agent($user_id);
        $is_str = '';
        foreach($user_list as $vo){
            $is_str.=$vo['next_user_id'].',';
        }
        $is_str = substr($is_str,0,-1);
        $partnerlist = Db::name('goods_order')->alias('o')
            ->join('user u','u.id = o.user_id')
            ->field('o.pay_time,o.pay_money,u.user_nickname')
            ->where('o.user_id','in',$is_str)
            ->where('o.status','neq',0)
            ->select();
        json_msg_data(1,'ok',$partnerlist);
    }
}